// 导入枚举库
import enumObj from '@/utils/enumerate/cruds'

//性别
export const personnelsex = val => {
  // 去枚举库里根据传递过来的数字找到对应的对象
  const obj = enumObj.consex.find(v => v.id == val)
  return obj.value
}
//学历
export const personducation = val => {
  // 去枚举库里根据传递过来的数字找到对应的对象
  const obj = enumObj.coneducation.find(v => v.id == val)
  return obj.value
}
//证件类型
export const personsNumber = val => {
  // 去枚举库里根据传递过来的数字找到对应的对象
  const obj = enumObj.conpapersNumber.find(v => v.id == val)
  return obj.value
}
// 过滤时间戳-到日期
export const formData = function (originVal) {
  const dt = new Date(originVal)

  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')

  return `${y}-${m}-${d}`
}
// 过滤器时间戳-到秒数
export const formDatas = function (originVal) {
  const dt = new Date(originVal)

  const y = dt.getFullYear()
  const m = (dt.getMonth() + 1 + '').padStart(2, '0')
  const d = (dt.getDate() + '').padStart(2, '0')

  const hh = (dt.getHours() + '').padStart(2, '0')
  const mm = (dt.getMinutes() + '').padStart(2, '0')
  const ss = (dt.getSeconds() + '').padStart(2, '0')

  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}

// 过滤合同起止时间
export const getymd = function (dateStr) {
  if (dateStr) {
    // console.log('dateStr', dateStr)
    let x = dateStr.split('--')
    let y = new Date(x[0])
    let z = new Date(x[1])

    const a = y.getFullYear()
    const b = (y.getMonth() + 1 + '').padStart(2, '0')
    const c = (y.getDate() + '').padStart(2, '0')

    const d = z.getFullYear()
    const e = (z.getMonth() + 1 + '').padStart(2, '0')
    const f = (z.getDate() + '').padStart(2, '0')
    return `${a}年-${b}月-${c}日 - ${d}年-${e}月-${f}日`
  }
}

export const congetymd = function (dateStr) {
  if (dateStr) {
    // console.log('dateStr', dateStr)
    let x = dateStr.split(',')
    let y = new Date(x[0])
    let z = new Date(x[1])

    const a = y.getFullYear()
    const b = (y.getMonth() + 1 + '').padStart(2, '0')
    const c = (y.getDate() + '').padStart(2, '0')

    const d = z.getFullYear()
    const e = (z.getMonth() + 1 + '').padStart(2, '0')
    const f = (z.getDate() + '').padStart(2, '0')
    return `${a}年-${b}月-${c}日 - ${d}年-${e}月-${f}日`
  }
}

/* 是否身份证号码*/
export function validateId (rule, value, callback) {
  const reg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/
  if (value == '' || value == undefined || value == null) {
    callback()
  } else {
    if (!reg.test(value) && value != '') {
      callback(new Error('请输入正确的身份证号码'))
    } else {
      callback()
    }
  }
}

/* 是否手机号码*/
export function validatePhone (rule, value, callback) {
  const reg = /^[1][3,4,5,7,8][0-9]{9}$/
  if (value == '' || value == undefined || value == null) {
    callback()
  } else {
    if (!reg.test(value) && value != '') {
      callback(new Error('请输入正确的电话号码'))
    } else {
      callback()
    }
  }
}

// 座机号码+手机号
export function validatePhonespecial (rule, value, callback) {
  if (value === '') {
    callback(new Error('联系电话不可为空！'))
  } else {
    let regPone = null
    let mobile = /^1(3|4|5|6|7|8|9)\d{9}$/ //最新16手机正则
    let tel = /^(0\d{2,3}-){0,1}\d{7,8}$/ //座机
    if (value.charAt(0) == 0) {
      // charAt查找第一个字符方法，用来判断输入的是座机还是手机号
      regPone = tel
    } else {
      regPone = mobile
    }
    if (!regPone.test(value)) {
      callback(new Error('请输入正确的电话格式！'))
    }
    callback()
  }
}
